<?php

define("APP_PATH", "./application/");
define("LIBS_PATH", "./library/");

// Include the system files
require_once LIBS_PATH . 'system/includes.php';

// Include the config file
require_once APP_PATH . 'config/Config.php';

// Include the main Propel script
require_once LIBS_PATH . 'propel/runtime/lib/Propel.php';

// Include the main Smarty script
require_once LIBS_PATH . 'smarty/Smarty.class.php';

// Initialize Propel with the runtime configuration
Propel::init(APP_PATH . "config/db-conf.php");

// Add the generated 'classes' directory to the include path
set_include_path(APP_PATH . "models" . PATH_SEPARATOR . get_include_path());

$config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
$config->setParameter('debugpdo.logging.details.time.enabled', true);
$config->setParameter('debugpdo.logging.details.mem.enabled', true);
$config->setParameter('debugpdo.logging.details.method.enabled', true);
$con = Propel::getConnection();
$con->useDebug(true);

$smarty = new Smarty();
$smarty->template_dir = APP_PATH . 'templates/';
$smarty->compile_dir  = APP_PATH . 'templates_c/';
$smarty->config_dir   = APP_PATH . 'configs/';
$smarty->cache_dir    = APP_PATH . 'cache/';

if (isset($_GET['controller'])) {
	require_once APP_PATH . 'controllers/' . $_GET['controller'] . '.php';
	$controllerName = ucfirst($_GET['controller']) . 'Controller';
	$controller = new $controllerName();
	$controller->init($smarty, ucfirst($_GET['controller']));
	if (isset($_GET['action'])) {
		$controller->setAction($_GET['action']);
		$action = $_GET['action'] . 'Action';
	}
	else {
		$controller->setAction($action = Config::getConfig()->controller->defaultAction);
		$action = Config::getConfig()->controller->defaultAction . 'Action';
	}
}
else {
	require_once APP_PATH . 'controllers/Index.php';
	$controllerName = Config::getConfig()->controller->defaultController . 'Controller';
	$controller = new $controllerName();
	$controller->init($smarty, Config::getConfig()->controller->defaultController, Config::getConfig()->controller->defaultAction);
	$action = Config::getConfig()->controller->defaultAction . 'Action';
}
$controller->before();
$controller->$action();
$controller->after();
